﻿using QuickManager.Code.Config.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickManager.Code.Config
{
    public class ConfigManager
    {
        public static ConfigModel GetConfig(string name)
        {

            using (SQLiteDataContext context = DB.GetContext())
            {
                ConfigModel configInfo = context.GetTable<ConfigModel>()
                    .ToList()
                    .FirstOrDefault(m => m.ConfigName == name);
                return configInfo;
            }
        }

        public static void SaveConfig(ConfigModel configInfo)
        {

            using (SQLiteDataContext context = DB.GetContext())
            {
                ConfigModel oldConfig = context.GetTable<ConfigModel>()
                    .ToList()
                    .FirstOrDefault(m => m.ConfigName == configInfo.ConfigName);
                if (oldConfig == null)
                {
                    context.GetTable<ConfigModel>().InsertOnSubmit(configInfo);
                }
                else
                {
                    oldConfig.ConfigValue = configInfo.ConfigValue;
                }
                context.SubmitChanges();
            }
        }

    }
}
